let SQLLITE = {
	/**
	 * 本地存储 -- 存储文件统一使用这个方法
	 * @param {Object} tabname 表名
	 * @param {Object} id id 传 "" 或 null
	 * @param {Object} type 业务类型 0其他类型（保护压板，变压器调压等）1 工作台类型
	 * @param {Object} process 流程类型  当前数据属于那个流程 当type == 1 时的流程类型（护压板，变压器调压等））
	 * @param {Object} data 需要存储的数据
	 * @param {Object} fun 回调函数
	 */
	localStorage:function(tabname,id,type,process,data,fun){
		let typeNet = plus.networkinfo.getCurrentType()
		if(1 == typeNet){
			//创建表
			SQLLITE.createTab(tabname,function(createRet){
				// 创建成功或表已存在则继续下一步操作
				//删除之前的数据
				SQLLITE.clearDB(tabname,function(clearRet){
					//存入离线数据
					SQLLITE.editTab(tabname,id,type,process,data,function(ret){
						//离线存储成功
						fun("localStorage Success")
					},function(err){
						// 离线存储失败
						fun("localStorage Fial")
					})
				},function(clearErr){
					// 离线存储失败--清理表失败
					fun("localStorage Fial--clear")
				})
			},function(createErr){
				// 创建失败 则终止数据库操作
				// 离线存储失败 -- 创建表失败
				fun("localStorage Fial--Create")
			})
		}else{
			//INTEL 表示当前有网络连接
			fun("INTEL")
		}
	},
	/**
	 * 打开数据库
	 */
	openDB:function(success,fail){
		let param = {}
		param.name = "zqDB";
		param.path= "_doc/zq.db";
		param.success = success;
		param.fail = fail;
		let isOpen = plus.sqlite.isOpenDatabase(param)
		if(!isOpen){
			plus.sqlite.openDatabase(param);
		}else{
			mui.toast("数据库处于打开状态！")
		}

	},
	/**
	 * 关闭数据库
	 */
	closeDB:function(success,fail){
		let param = {}
		param.name = "zqDB";
		param.path= "_doc/zq.db"
		param.success = success;
		param.fail = fail;
		let isOpen = plus.sqlite.isOpenDatabase(param)
		if(isOpen){
			plus.sqlite.closeDatabase(param);
		}
	},

	/**
	 * 创建表
	 * @param {String} tabName 表名
	 * @param {fun} success 成功回参
	 * @param {fun} fail 失败回参
	 * @param {int} id 主键自增
	 * @param {String} type 主体类型 0其他类型（保护压板，变压器调压等）1 工作台类型
	 * @param {String} process 流程类型（当type == 1 时的流程类型（护压板，变压器调压等））
	 * @param {String} data 数据 JSONString 类型 
	 */
	createTab:function(tabName,success,fail){
		let param = {};
		param.name = "zqDB";
		param.sql = "create table if not exists '"+tabName+"'(id INTEGER primary key AUTOINCREMENT,type VCHAR(110), process VCHAR(110), data VCHAR(5000))";
		param.success = success;
		param.fail = fail;
		
		SQLLITE.getSqlData(tabName,function(ret){
			success("表"+tabName+"已存在");
		},function(err){
			plus.sqlite.executeSql(param);
		})
	},
	deleteTab:function(success,fail){
		let param = {};
		param.name = "zqDB";
		param.sql = "drop table zqtab";
		param.success = success;
		param.fail = fail;
		plus.sqlite.executeSql(param);
	},
	/**
	 * 修改表
	 */
	editTab:function(tabname,id,type,process,data,success,fail){
		let param = {};
		param.name = "zqDB";
		param.sql = "insert into "+tabname+" values(NULL,'"+type+"',"+process+"," + "'"+data+"')";
		param.success = success;
		param.fail = fail;
		plus.sqlite.executeSql(param);
	},
	/**
	 * 清空表
	 * @param {Object} tabName 表名
	 * @param {Object} success 成功回调
	 * @param {Object} fail 失败回调
	 */
	clearDB(tabName,success,fail){
		let param = {};
		param.name = "zqDB";
		param.sql = "delete from '"+tabName+"'";
		param.success = success;
		param.fail = fail;
		plus.sqlite.executeSql(param);
	},
	/**
	 * 查询表
	 */
	getSqlData:function(tabName,success,fail){
		let param = {};
		param.name = "zqDB";
		param.sql = "select * from '"+tabName+"'";
		param.success = success;
		param.fail = fail;
		plus.sqlite.selectSql(param);
	},
	/**
	 * 查询表是否已经存在
	 */
	isTab:function(tabName,success,fail){
		let sql = "select * from information_schema.TABLES where TABLE_NAME = '"+tabName+"';"
		let param = {};
		param.name = "zqDB";
		param.sql = sql;
		param.success = success;
		param.fail = fail;
		plus.sqlite.selectSql(param);
	}
}